// pages/personal/myorder/myorder.js
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curHdIndex: 0,
        orderState: 10000,
        currentPage: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.addressInfo();
    },
    onShow: function() {
        var that = this;
        that.setData({
            orderList: [],
            currentPage: 1
        })
        that.onOrderList(that.data.orderState,that.data.currentPage)
    },
    onOrderList: function (orderState, currentPage) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/order/my_order_list", {
            order_state: orderState,
            page: currentPage,
            store_id: wx.getStorageSync("userType").storeid
        }, function(res) {
            utils.hideLoading();
            var allpost = that.data.orderList;
            if (allpost == undefined) {
                allpost = [];
            }
            for (var i in res.datas) {
                res.datas[i].number = 0;
                allpost.push(res.datas[i])
            }
            if (res.datas.length < 10) {
                that.setData({
                    tishi_title: '已加载全部',
                });
            } else {
                that.setData({
                    tishi_title: '加载更多',
                });
            }
            that.setData({
                orderList: allpost,
                ready: res.datas.length == 0 ? 1 : 2,
            })
        })

    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.currentPage + 1
            that.setData({
                currentPage: page,
            })
            that.onOrderList(that.data.orderState, page)
        }
    },
    //tab切换
    tab: function(e) {
        var stateId = e.currentTarget.dataset.stateid;
        var dataId = e.currentTarget.id;
        var curHdIndex = dataId;
        this.setData({
            curHdIndex: curHdIndex,
            orderState: stateId,
            currentPage: 1,
            orderList: []
        })
        this.onOrderList(stateId,this.data.currentPage)
    },
    //订单详情
    onOrderDetailsTap: function(e) {
        wx.navigateTo({
            url: '../personal/myorderdetails/myorderdetails?id=' + e.currentTarget.id,
        })
    },
    //订单评价
    onEvaluationTap: function(e) {
        wx.navigateTo({
            url: '../../evaluation/evaluation?id=' + e.currentTarget.dataset.id,
        })
    },
    //申请售后
    onAftersaleTap: function(e) {
        wx.navigateTo({
            url: '../personal/aftersale/aftersale?id=' + e.currentTarget.dataset.id,
        })
    },
    //跳入物流进度
    onLogisticsProgressTap: function(e) {
        wx.navigateTo({
            url: '../personal/logisticsprogress/logisticsprogress?orderid=' + e.currentTarget.dataset.orderid
        })
    },

    //确认收货
    onReceivingTap: function(e) {
        var that = this;
        wx.showModal({
            title: '确认收货',
            content: '您是确认收货吗',
            success(res) {
                if (res.confirm) {
                    utils.http("/api/v1/order/ok_order", {
                        order_id: e.target.dataset.id
                    }, function(res) {
                        if (res.state == 1) {
                            that.setData({
                                currentPage: 1,
                                orderList: []
                            })
                            that.onOrderList(that.data.orderState)
                        } else {
                            utils.message(res.datas)
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    //提醒发货
    deliveryTap: function(e) {
        var that = this;
        utils.http("/api/v1/order/ques_delivery", {
            order_id: e.currentTarget.dataset.id
        }, function(res) {
            if (res.state == 1) {
                wx.showToast({
                    title: "已提醒成功",
                    icon: "none"
                })
            } else {
                utils.message(res.datas)
            }
        })
    },
    //取消订单
    cancelOrderTap: function(e) {
        var that = this;
        utils.http("/api/v1/order/ok_order", {
            order_id: e.currentTarget.dataset.id
        }, function(res) {
            if (res.state == 1) {
                wx.showToast({
                    title: "您已取消订单",
                    icon: "none",
                    currentPage: 1,
                    orderList: []
                })
                that.onOrderList(that.data.orderState,1)
            } else {
                utils.message(res.datas)
            }
        })
    },
    addressInfo: function () {
        var that = this;
        utils.http("/api/v1/storemanage/store_address", {
            store_id: wx.getStorageSync("userType").storeid
        }, function (res) {
            that.setData({
                defaultAddress: res.datas,
            })
        })
    },

    //去付款
    goConfirMorderTap: function(e) {
        var that = this;
        utils.showLoading();
        var index = e.currentTarget.dataset.index;
        var listCar=that.data.orderList[index].snap_items;
        var orderCar = [];
        var goodsList = [];
        for (var i in listCar) {
                goodsList.push({
                    goods_id: listCar[i].id,
                    num: listCar[i].num
                });
            
        }
        utils.http("/api/v1/Storemanage/add_order", {
            store_id: wx.getStorageSync("userType").storeid,
            goods_list: goodsList,
            address_id: that.data.defaultAddress.id,
        }, function(res) {
            console.log(res);
            utils.hideLoading();
            if (res.state == 1) {
                wx.requestPayment({
                    timeStamp: res.datas.timeStamp,
                    nonceStr: res.datas.nonceStr,
                    package: res.datas.package,
                    signType: res.datas.signType,
                    paySign: res.datas.paySign,
                    success(res) {
                        that.shoppClear();
                        wx.redirectTo({
                            url: '../ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
                        })
                    },
                    fail(res) {
                        that.setData({
                            buttonClicked: false
                        })
                    }
                })
            } else {
                utils.message(res.datas);
            }
        })




    },
})